package com.hod.client.lodging;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gears.client.database.DatabaseException;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.DateFieldDef;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.CellMetadata;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.Renderer;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
import com.gwtextux.client.data.PagingMemoryProxy;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.charge.ChargeCRUD;
import com.hod.client.client.Client;
import com.hod.client.frontend.AbstractGrid;
import com.hod.client.reserves.Reservation;
import com.hod.client.room.Room;

public class LodgingGrid extends AbstractGrid {
	
	private GearsDataStore dsClient = null;
	private GearsDataStore dsRoom = null;    
    
    private Renderer rndrClient = new Renderer() {
        public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
        	Client o;
			try {
				o = (Client)dsClient.findById(new Integer(record.getAsString("idClient")));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				return "";
			} catch (DatabaseException e) {
				// TODO Auto-generated catch block
				return "";
			} 
            return o.getName()+" "+o.getSurname();
        }
    };
    private Renderer rndrRoom = new Renderer() {
        public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
        	Room o;
			try {
				o = (Room)dsRoom.findById(new Integer(record.getAsString("idRoom")));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				return "";
			} catch (DatabaseException e) {
				// TODO Auto-generated catch block
				return "";
			} 
            return o.getNumber();
        }
    };

	private static RecordDef recordDef = new RecordDef(
            new FieldDef[]{
                    new IntegerFieldDef("idLodging"),
                    new IntegerFieldDef("idClient"),                    
                    new IntegerFieldDef("idReservation"),
                    new IntegerFieldDef("idRoom"),
                    new DateFieldDef("checkIn"),
                    new DateFieldDef("checkOut"),                    
                    new FloatFieldDef("total"),
               }
    );

	

	public LodgingGrid() {      
    }

    public LodgingGrid(boolean small,GearsDataStore ds) {
    	this.ds = ds;
    	this.dsClient = (GearsDataStore) GWT.create(Client.class);
    	this.dsRoom = (GearsDataStore) GWT.create(Room.class);
    	
    	columns = new BaseColumnConfig[]{
                new ColumnConfig("idLodging", "idLodging", 60, true, null, "idLodging"),    	
                new ColumnConfig("idClient", "idClient", 60, true,rndrClient, "idClient"),
                new ColumnConfig("idReservation", "idReservation", 60, true,null, "idReservation"),
                new ColumnConfig("idRoom", "idRoom", 60, true,rndrRoom, "idRoom"),
                new ColumnConfig("checkIn", "checkIn", 60, true,null, "checkIn"),
                new ColumnConfig("checkOut", "checkOut", 60, true,null, "checkOut"),
                new ColumnConfig("total", "total", 60, true, null, "total")
        };
    	
    	getData();

        ColumnModel columnModel = getColumnConfigs();
        setColumnModel(columnModel);

        setFrame(true);
        setStripeRows(true);
        setIconCls("grid-icon");      

        createPopUp();
        createPager(); 
    }
    protected void getData(){    	
    	getData("");
    }
    
    protected void getData(String idRoomType){
    	PagingMemoryProxy proxy = null;
		try {
			GearsQuery q = this.ds.getNamedQuery(Lodging.FIND_ALL);
			Lodging[] data = (Lodging[]) q.execute();
			Lodging rs = null;
			Object[][]data2 = new Object[data.length][];
			for (int i = 0; i < data.length; i++) {
                rs = data[i];	               
                data2[i] = new Object[]{rs.getIdLodging(),rs.getIdClient(),rs.getIdReservation(),rs.getIdRoom(),rs.getCheckIn(),rs.getCheckOut(),rs.getTotal()};
            }
			proxy = new PagingMemoryProxy(data2);
			
			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			//Window.alert(e.getMessage());
		} 

        ArrayReader reader = new ArrayReader(recordDef);
        store = new Store(proxy, reader);
        store.load(0,numberRecords);
        setStore(store);
    }
    
    public void createGridMenu(){
    	super.createGridMenu();
    	gridMenu.addItem(new Item("Charge",new BaseItemListenerAdapter() { 
	           public void onClick(BaseItem item, EventObject e) { 
	        	   ChargeCRUD charge = new ChargeCRUD();
	        	   Window wndCharge = new Window();
	        	   wndCharge.add(charge.createFormPanel());
	        	   charge.setIdLodging(getSelectionModel().getSelected().getAsInteger("idLodging"));
	        	   wndCharge.show();
	        	} 
	         }));  
    	
    }

}
